package com.yyfax.ph.web;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class WebPresenter extends Presenter<WebModel, WebPresenter.View> {

	private static final String TAG = WebPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<WebPresenter> {
		@Override
		public WebPresenter create() {
			return new WebPresenter();
		}
	}

	public interface View extends IView {
		void getUrlSuccess(String url);

		void getUrlFailure();
	}

	public View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new WebModel();
		EventAgent.register(this);
		mRequestPage = view.getClass().getName();
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getWebUrl(int type) {
		execute(String.valueOf(type),FUNCTIONAL_CODE, false,null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getUrlResult(WebModel.WebResult webResult) {
		if (mRequestPage.equals(webResult.requestPage)) {
			if (isResponseSuccess(webResult)) {
				mView.getUrlSuccess(webResult.getContent());
			} else {
				mView.getUrlFailure();
				CLog.i(TAG, "getUrlResult: " + webResult.toString());
			}
		}
	}
}
